import java.util.*;

public class ShipManager extends Thread
{
  private ArrayList <Ship>ships;
  boolean pause;
  GameWindow gw;
  
  public ShipManager(GameWindow gw)
  {
    ships=new ArrayList<Ship>();
    pause=false;
    this.gw=gw;
  }
  
  public void addShip(Ship s)
  {
    ships.add(s);
  }
  
  public void removeShip(Ship s)
  {
    ships.remove(s);
  }
  
  public ArrayList<Ship> getShips()
  {
    return ships;
  }
  
  public void run()
  {
    while (true)
    {
      if (!pause)
      {
        for (Ship s:ships)
        {
          s.advanceTurn();
          if (s.battery < s.maxBattery)
          {
            if (s!=gw.playerShip||!gw.mouseDown)
              s.battery ++;
            
          }
          if (s!=gw.playerShip)
            s.automate();
          if (s.hp<=0)
          {
            ships.remove(s);
            System.out.println("Ship died!");
            break;
          }
        }
        try
        {
          Thread.sleep(10);
        }
        catch (Exception e){}
      }
    }
  }
}